<template>
  <div>
    <div class="strategyFatherBox">
      <el-button type="success" size='small' @click="startReport('enable')" class="enableReClick">{{$t('开启任务')}}</el-button>
      <el-button type="danger" size='small' @click="startReport('disable')" class="disableReClick">{{$t('关闭任务')}}</el-button>
    </div>
    <el-table size='mini' :data="tableData" :header-cell-style="{'text-align':'center'}" :cell-style="{'text-align':'center'}" style="width: 100%" @selection-change="handleSelectionChange">
      <el-table-column type="selection" align='center' width="55"></el-table-column>
      <el-table-column prop="name" :label="$t('报表任务名称')" min-width="120">
        <template slot-scope="scope">
          <tooltip :html="scope.row.show_name" :content="scope.row.show_name" class="wid190" :refName="scope.row.show_name">
          </tooltip>
        </template>
      </el-table-column>
      <el-table-column prop="status" :label="$t('任务状态')" min-width="100">
        <template slot-scope="scope">
          {{ $t(statusList[scope.row.status]) }}
        </template>
      </el-table-column>
      <el-table-column prop="prot" :label="$t('报表周期')" min-width="100">
        <template slot-scope="scope">
          {{ $t(timeTypeList[scope.row.time_type]) }}
        </template>
      </el-table-column>
      <el-table-column prop="type" :label="$t('自定义模板')" min-width="120">
        <template slot-scope="scope">
          <tooltip :html="diyTemplateShow(scope.row.reftemplate, '<br/>')" :content="diyTemplateShow(scope.row.reftemplate, ';')" class="wid190" :refName="diyTemplateShow(scope.row.reftemplate)">
          </tooltip>
        </template>
      </el-table-column>
      <el-table-column align='center' width='100' fixed="right" :label="$t('操作')">
        <template slot-scope="scope">
          <el-button type="success" plain size="mini" @click="dialogVisible(scope.row)" :class="'detailsRe' + (scope.$index + 1)">{{$t('查看详情')}}</el-button>
        </template>
      </el-table-column>
    </el-table>
    <DiyReportDetails v-if="diyReportVisible" ref="refDiyReportVisible"></DiyReportDetails>
  </div>
</template>

<script>
import DiyReportDetails from './DiyReportDetails'
import { getReportArray, setReportStatus } from '@/api/system/logReport/diyReport'
export default {
  name: 'ServerRe',
  components: {
    DiyReportDetails
  },
  data () {
    return {
      diyReportVisible: false,
      tableData: [],
      statusList: {
        'enable': '开启',
        'disable': '关闭'
      },
      timeTypeList: {
        'single': '历史报表',
        'day': '周期性报表',
        'week': '周期性报表',
        'month': '周期性报表'
      },
      selectList: []
    }
  },
  mounted () {
    this.listData()
  },
  methods: {
    listData () {
      this.tableData = []
      getReportArray().then(res => {
        if (res.status === 0 && res.info && res.info.length > 0) {
          for (let item of res.info) {
            if (item.level === 'default') {
              this.tableData.push(item)
            }
          }
        }
      })
    },
    diyTemplateShow (value, str) {
      let reStr = ''
      if (value && value.length > 0) {
        for (let item of value) {
          reStr += item + str
        }
      }
      return reStr
    },
    startReport (data) {
      let flag = false
      for (let item of this.selectList) {
        if (item.status !== data) {
          flag = true
        }
      }
      if (!flag) {
        this.$messages.error(data === 'enable' ? this.$t('请至少选择一条关闭的报表任务进行启用') : this.$t('请至少选择一条开启的报表任务进行禁用'))
        return
      }
      if (this.selectList && this.selectList.length > 0 && flag) {
        let str = []
        for (let item of this.selectList) {
          str.push(item.name)
        }
        setReportStatus({ name: str.join('@#@'), status: data }).then(res => {
          if (res.status === 0) {
            this.$message({
              message: this.$t('执行成功'),
              type: 'success'
            })
            this.listData()
          }
        })
      } else {
        this.$messages.error(this.$t('请至少选择一条数据'))
      }
    },
    handleSelectionChange (data) {
      this.selectList = data
    },
    dialogVisible (data) {
      this.diyReportVisible = true
      this.$nextTick(() => {
        this.$refs.refDiyReportVisible.init(data)
      })
    }
  }
}
</script>

<style scoped>
</style>
